﻿/*********************************************************************
 版权所有: 版权所有(C) 2009 ,熙软研发部
 模块编号:  AreaManage.aspx
 作    者:高文华
 编写日期:2010-02-22
 审    核:
 审核日期:
 描    述:区域列表
...........................................................................
如果模块有修改，则每次修改添加以下注释(以系统上线后为基准)：

 *********************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cisoft.YiWu.BLL;
using Cisoft.YiWu.Model;

public partial class Business_YiWu_AreaManage : WebUI.Page.RecursivePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            #region 管理员登录Session 判断 整合新逻辑  代码放置后请检查本页面中是否存在Login.aspx字眼等相关逻辑
            ////登陆起时判断\ Session过期     整合新逻辑                                            //后续补全
            //if (Session["UserID"] != null && Session["UserID"].ToString() != "")
            //{ }
            //else
            //{
            //    string cmsinteurl = ConfigMg.UIConfig.cisIntegrationcms.ToString();
            //    Response.Write("<script language=javascript>parent.window.parent.window.location.href='" + cmsinteurl + "'</script>");
            //}/* end of if*/
            #endregion

            IsOut();
            gvitemBind(gvitem);
        }

        //删除确认提示信息
        butDelete.Attributes.Add("onclick", "return confirm('您确定要删除吗？相关的附属信息也将一并删除')");
    }

    #region 全选事件 protected void All_check(object sender, EventArgs e)
    /// <summary>
    /// 全选按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void All_check(object sender, EventArgs e)
    {
        rebackLookout();//清空查询框，后续补全
        for (int i = 0; i < gvitem.Rows.Count; i++)
        {
            ((CheckBox)gvitem.Rows[i].FindControl("chkSelect")).Checked = this.ChkAll.Checked;
        }
    }
    #endregion

    #region 删除事件 protected void butDelete_Click(object sender, ImageClickEventArgs e)
    /// <summary>
    /// 删除Event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void butDelete_Click(object sender, ImageClickEventArgs e)
    {

        AreaInfoBO bllAreaInfoDel = new AreaInfoBO();

        rebackLookout();//清空查询框
        bool Mgtest = false;//判断是否有选择
        for (int i = 0; i < this.gvitem.Rows.Count; i++)
        {
            bool isChecked = ((CheckBox)gvitem.Rows[i].FindControl("chkSelect")).Checked;
            if (isChecked)//判断是否选择
            {
                Mgtest = true;
                int intID = Convert.ToInt32(((Label)gvitem.Rows[i].FindControl("LblID")).Text);
                try
                {
                    DeleteAreaInfo(intID);
                    bllAreaInfoDel.Delete(intID);
                    //DeleteAllCustomers(intID);
                }
                catch (Exception ex)
                {
                    string errortip = "记录ID：" + intID.ToString() + "删除出现异常，请通知管理员，以下为出错提示信息：" + ex.Message.ToString();
                    //PageError(errortip, "AreaManage.aspx", "返回操作", "");

                    string scriptStr = "location.replace('Rederect.aspx?type=error&message='" + errortip + "')";
                    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", scriptStr, true);
                }

            }

        }

        //没有选择项时的提示
        if (!Mgtest)
        {
            //重新绑定显示
            gvitemBind();
            ChkAll.Checked = false;
            //PageError("请选择要删除的记录！", "AreaManage.aspx", "返回操作", "");
            string scriptStr = "location.replace('Rederect.aspx?type=choose')";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", scriptStr, true);
        }
        else
        {
            //重新绑定显示
            gvitemBind();
            ChkAll.Checked = false;
            //PageRight("恭喜，删除操作成功", "AreaManage.aspx", "返回继续操作", "");
            string scriptStr = "location.replace('Rederect.aspx?type=seccuse')";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", scriptStr, true);
        }
    }
    #endregion

    #region 删除相关信息区域
    /// <summary>
    /// 删除相关信息区域
    /// </summary>
    /// <param name="Proposalid">医院编号</param>
    private void DeleteAreaInfo(int Areaid)
    {
        Cisoft.YiWu.BLL.HospitalInfoBO bllHospitalInfodel2 = new Cisoft.YiWu.BLL.HospitalInfoBO();

        DataSet ds = new DataSet();
        ds = bllHospitalInfodel2.GetList(" HI_HospitalAreaID=" + Areaid);
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    //删除逻辑
                    DeleteHospitalInfo(int.Parse(ds.Tables[0].Rows[0]["HI_HospitalID"].ToString()));
                }

                //try
                //{
                //    MPRBll.Delete(MPRid);
                //}
                //catch { }
            }
        }/* end of if(ds!==null)*/

    }
    #endregion

    #region 删除相关信息医院
    /// <summary>
    /// 删除相关信息医院
    /// </summary>
    /// <param name="Proposalid">医院编号</param>
    private void DeleteHospitalInfo(int Hospitalid)
    {
        Cisoft.YiWu.BLL.DepartmentInfoBO bllDepartmentInfodel2 = new Cisoft.YiWu.BLL.DepartmentInfoBO();

        DataSet ds = new DataSet();
        ds = bllDepartmentInfodel2.GetList(" DI_DepartmentHospitalID=" + Hospitalid);
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    //删除逻辑
                    DeletePlanInfo(int.Parse(ds.Tables[0].Rows[0]["TS_SliceID"].ToString()));
                }

                //try
                //{
                //    MPRBll.Delete(MPRid);
                //}
                //catch { }
            }
        }/* end of if(ds!==null)*/

    }
    #endregion


    #region 删除相关信息部门
    /// <summary>
    /// 删除相关信息部门
    /// </summary>
    /// <param name="Proposalid">部门编号</param>
    private void DeletePlanInfo(int Departmentid)
    {
        Cisoft.YiWu.BLL.TimeSliceBO bllTimeSlicedel2 = new Cisoft.YiWu.BLL.TimeSliceBO();
        Cisoft.YiWu.BLL.PlanInfoBO bllPlanInfodel2 = new Cisoft.YiWu.BLL.PlanInfoBO();
        DataSet ds = new DataSet();
        ds = bllTimeSlicedel2.GetList(" TS_SliceDepartmentID=" + Departmentid);
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    bllPlanInfodel2.Delete(" PI_PlanSliceID=" + ds.Tables[0].Rows[0]["TS_SliceID"].ToString());
                }

                //删除时段
                bllTimeSlicedel2.Delete(" TS_SliceDepartmentID=" + Departmentid);

                //try
                //{
                //    MPRBll.Delete(MPRid);
                //}
                //catch { }
            }
        }/* end of if(ds!==null)*/

    }
    #endregion

    #region 显示区域列表 private void gvitemBind()
    /// <summary>
    /// 显示区域列表
    /// </summary>
    private void gvitemBind()
    {
        string strKeyContent = this.txtPropasal.Text.Trim().ToString();

        DataSet ds = new AreaInfoBO().GetListBykey(ddl_SearchCondition.SelectedValue, strKeyContent, "");

        DataView dvforalldata = new DataView();
        ViewState["numbers"] = ds.Tables[0].Rows.Count.ToString();
        dvforalldata.Table = ds.Tables[0];
        //dvforalldata.Sort = "AI_AreaOrder desc";
        gvitem.DataSource = dvforalldata;

        gvitem.DataBind();
        butState();   //显示分页按钮状态
        ShowStats(); //显示页数
    }

    private void gvitemBind(GridView gv)
    {
        string strKeyContent = this.txtPropasal.Text.Trim().ToString();

        //DataSet ds = new AreaBO().GetListBykey(ddl_SearchCondition.SelectedValue, strKeyContent);
        GetList(gv, ddl_SearchCondition.SelectedValue, strKeyContent);
        ViewState["numbers"] = gv.Rows.Count;
        butState();   //显示分页按钮状态
        ShowStats(); //显示页数
    }

    #endregion

    #region 翻页事件 protected void PagerButtonClick(object sender, EventArgs e)
    /// <summary>
    /// 翻页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        string arg = ((LinkButton)sender).CommandArgument.ToString();
        switch (arg)
        {
            case "next":
                if (gvitem.PageIndex < (gvitem.PageCount - 1))
                {
                    gvitem.PageIndex += 1;
                }
                break;
            case "prev":
                if (gvitem.PageIndex > 0)
                {
                    gvitem.PageIndex -= 1;
                }
                break;
            case "last":
                gvitem.PageIndex = (gvitem.PageCount - 1);
                break;
            default:
                gvitem.PageIndex = System.Convert.ToInt32(arg);
                break;
        }/* end of  switch (arg) */
        gvitemBind();

    }
    #endregion

    #region 翻页按钮状态 private void butState()
    /// <summary>
    /// 翻页按钮状态
    /// </summary>
    private void butState()
    {
        if (this.gvitem.PageIndex < 1)
        {
            this.btnFirst.Enabled = false;
            this.btnPrev.Enabled = false;
        }
        else
        {
            this.btnFirst.Enabled = true;
            this.btnPrev.Enabled = true;
        }

        if ((this.gvitem.PageIndex + 2) > this.gvitem.PageCount)
        {
            this.btnLast.Enabled = false;
            this.btnNext.Enabled = false;
        }
        else
        {
            this.btnLast.Enabled = true;
            this.btnNext.Enabled = true;
        }
    }
    #endregion

    #region 显示页数 private void ShowStats()
    /// <summary>
    /// 显示页数
    /// </summary>
    private void ShowStats()
    {
        lblCurrentIndex.Text = "第 <font color='red'>" + (this.gvitem.PageIndex + 1).ToString() + "</font> 页";
        string Pagecount = "";
        int i = int.Parse(this.gvitem.PageCount.ToString());
        if (i == 0)
        {
            Pagecount = "1";
        }
        else
        {
            Pagecount = this.gvitem.PageCount.ToString();
        }
        lblNumbers.Text = "总共 " + ViewState["numbers"].ToString() + " 条记录 ";
        lblPageCount.Text = "共 <font color='red'>" + Pagecount + "</font> 页";
    }
    #endregion

    #region gridview中的事件

    #region RowDataBound事件 protected void gvitem_RowDataBound(object sender, GridViewRowEventArgs e)
    /// <summary>
    /// RowDataBound事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvitem_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //ID绑定
            Label LblID = (Label)e.Row.FindControl("LblID");

            //Label lbl_AreaList = e.Row.FindControl("lbl_AreaList") as Label;
            //if (lbl_AreaList.Text.Trim().Length > 15)
            //{
            //    string ss = lbl_AreaList.Text;
            //    lbl_AreaList.Text = "<span title='" + lbl_AreaList.Text + "'>" + ss.Substring(0, 14) + "...</span>";
            //}
            //else
            //{
            //    lbl_AreaList.Text = lbl_AreaList.Text.Replace("...", "");
            //}
            //GiftsBO gBO = new GiftsBO();
            //GiftsMO gMO = new GiftsMO();
            //string[] strs = lbl_AreaList.Text.TrimEnd(',').Split(',');
            //string strNew = "";

            //Label lbl_CustomerList = e.Row.FindControl("lbl_CustomerList") as Label;
            //DataSet dsforc = new Cisoft.Booking.BLL.DealerBO().GetList(" AreaID_FK =" + LblID.Text + " ");
            //List<string> namelist = new List<string>();
            //if (dsforc.Tables[0].Rows.Count > 0)
            //{
            //    foreach (DataRow row in dsforc.Tables[0].Rows)
            //    {
            //        namelist.Add(row["DealerName"].ToString());
            //    }
            //    lbl_CustomerList.Text = string.Join(",", namelist.ToArray());
            //}
            //if (string.IsNullOrEmpty(lbl_CustomerList.Text))
            //{
            //    lbl_CustomerList.Text = "暂无";
            //}
            //if (lbl_CustomerList.Text.Length > 18)
            //{
            //    string sss = lbl_CustomerList.Text;
            //    lbl_CustomerList.Text = "<span title='" + lbl_CustomerList.Text + "'>" + sss.Substring(0, 17) + "...</span>";
            //}
            //else
            //{
            //    lbl_CustomerList.Text = lbl_CustomerList.Text;
            //}


            //Label lbl_Leader = e.Row.FindControl("lbl_Leader") as Label;
            //DataSet ds = new AreaBO().GetDataSetByAreaID(LblID.Text);
            //if (ds.Tables[0].Rows.Count > 0)
            //{
            //    lbl_Leader.Text = ds.Tables[0].Rows[0]["us_realname"].ToString();
            //}
            //if (string.IsNullOrEmpty(lbl_Leader.Text))
            //{
            //    lbl_Leader.Text = "暂无";
            //}
        }

        //鼠标在列表行中停留时的样式
        for (int i = -1; i < gvitem.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#B5C9DD'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
    }
    #endregion

    #region gridview行命令
    protected void gvGroup_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "Group_expended")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = this.gvitem.Rows[index];

            string flag = ((Label)row.FindControl("LblID")).Text;
            if (iIsout[int.Parse(flag)] == 0)
            {
                iIsout[int.Parse(flag)] = 1;
            }
            else
            {
                iIsout[int.Parse(flag)] = 0;
            }
            gvitemBind(gvitem);
        }

    }
    protected void gvGroup_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton Lbtn_a = (LinkButton)e.Row.FindControl("lbl_aName");
            Lbtn_a.CommandArgument = e.Row.RowIndex.ToString();
        }
    }
    #endregion

    #region PageIndexChanging事件 protected void gvitem_PageIndexChanging(object sender, GridViewPageEventArgs e)
    /// <summary>
    /// PageIndexChanging事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvitem_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvitem.PageIndex = e.NewPageIndex;
    }
    #endregion

    #endregion

    #region  查询事件 protected void butlookout_Click(object sender, ImageClickEventArgs e)
    /// <summary>
    /// 查询事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void butlookout_Click(object sender, ImageClickEventArgs e)
    {
        if (!string.IsNullOrEmpty(txtPropasal.Text.Trim()))
        {
            gvitemBind();
        }
        else
        {
            gvitemBind(gvitem);
        }
    }
    #endregion

    #region 清空查询后的状态 private void rebackLookout()
    /// <summary>
    /// 清空查询后的状态
    /// </summary>
    private void rebackLookout()
    {
        txtPropasal.Text = "";
    }
    #endregion

    #region 添加 protected void ibtnxinzeng_Click(object sender, ImageClickEventArgs e)
    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnxinzeng_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("AreaAdd.aspx");
    }
    #endregion

    #region 删除时批量删除所有经销商 private void DeleteAllCustomers(int AreaID)
    /// <summary>
    /// 删除时批量删除所有经销商
    /// </summary>
    /// <param name="AreaID">区域标识ID</param>
    //private void DeleteAllCustomers(int AreaID)
    //{
    //    DealerBO dbo = new DealerBO();

    //    List<DealerMO> dmolist = dbo.GetModelList(" AreaID_FK = " + AreaID + " ");
    //    if (dmolist.Count > 0)
    //    {
    //        foreach (DealerMO mo in dmolist)
    //        {
    //            dbo.Delete(mo.DealerID);
    //        }
    //    }
    //}
    #endregion
}
